//
//  QuestionTableView.m
//  one·一个  tyw
//
//  Created by tyw on 15/9/9.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "QuestionTableView.h"
#import "DayNightLabel.h"
#import "DayNightImgView.h"
#import "QuestionTableViewCell.h"

@implementation QuestionTableView {
    DayNightLabel* likeCountLabel;
}

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        self.backgroundColor = [UIColor clearColor];
        
        self.delegate = self;
        self.dataSource = self;
        
        //注册
        [self registerClass:[QuestionTableViewCell class] forCellReuseIdentifier:kQuestionTableViewCellId];
    }
    
    return self;
}

#pragma mark - setDataDic
- (void)setDataDic:(NSDictionary *)dataDic {
    if (_dataDic != dataDic) {
        _dataDic = dataDic;
        
        [self reloadTableHeadFootView];         //刷新表视图的头部和尾部视图
        [self reloadData];                      //刷新表视图
    }
}

#pragma mark - reloadTableHeadFootView
- (void)reloadTableHeadFootView {
    //时间、问题、问题内容、回答者，作为头部视图
    UIView* headView = [[UIView alloc] init];
    //时间
    DayNightLabel* timeLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 0, kWidth-20, 20)];
    [timeLabel loadLabelColor];
    timeLabel.text = [Tools exchangeDate:_dataDic[@"strQuestionMarketTime"]];
    timeLabel.textAlignment = NSTextAlignmentLeft;
    timeLabel.font = [UIFont systemFontOfSize:10];
    [headView addSubview:timeLabel];
    //问题图标
    UIImageView* questionImgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 36, 36)];
    questionImgView.image = [UIImage imageNamed:@"que_img"];
    [headView addSubview:questionImgView];
    //问题
    DayNightLabel* quesLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(70, 30, kWidth-80, 40)];
    [quesLabel loadLabelColor];
    quesLabel.text = _dataDic[@"strQuestionTitle"];
    quesLabel.textAlignment = NSTextAlignmentLeft;
    quesLabel.font = [UIFont systemFontOfSize:15];
    quesLabel.numberOfLines = 0;
    [headView addSubview:quesLabel];
    //问题内容
    NSString* questionContent = _dataDic[@"strContAuthor"];
    CGFloat height = [Tools contentSizeWithMaxWidth:kWidth-20 text:questionContent fontOfSize:15].height;
    DayNightLabel* quesContenLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 70, kWidth-20, height+10)];
    [quesContenLabel loadLabelColor];
    quesContenLabel.text = questionContent;
    quesContenLabel.textAlignment = NSTextAlignmentLeft;
    quesContenLabel.font = [UIFont systemFontOfSize:14];
    [headView addSubview:quesContenLabel];
    //分割线
    DayNightImgView* line = [[DayNightImgView alloc] initWithFrame:CGRectMake(10, 80+height+4.5, kWidth-20, 1)];
    [line setImgName:@"que_line"];
    [headView addSubview:line];
    //回答图标
    UIImageView* answerImgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100+height, 36, 36)];
    answerImgView.image = [UIImage imageNamed:@"ans_img"];
    [headView addSubview:answerImgView];
    //回答标题
    DayNightLabel* answerTitleLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(70, 110+height, kWidth-80, 20)];
    [answerTitleLabel loadLabelColor];
    NSString* answerTitle = _dataDic[@"strAnswerTitle"];
    answerTitleLabel.text = [answerTitle stringByReplacingOccurrencesOfString:@"答" withString:@" 答 "];
    answerTitleLabel.textAlignment = NSTextAlignmentLeft;
    answerTitleLabel.font = [UIFont systemFontOfSize:15];
    [headView addSubview:answerTitleLabel];
    //设置头部视图的高度
    [headView setFrame:CGRectMake(0, 0, kWidth, 150+height)];
    self.tableHeaderView = headView;
    
    
    
    //责任编辑、赞, 作为尾部视图
    UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 90)];
    self.tableFooterView = footerView;
    //责任编辑
    DayNightLabel* contAuthorIntroduceLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 20, kWidth-20, 20)];
    [contAuthorIntroduceLabel loadLabelColor];
    contAuthorIntroduceLabel.text = _dataDic[@"sEditor"];
    contAuthorIntroduceLabel.textAlignment = NSTextAlignmentLeft;
    contAuthorIntroduceLabel.font = [UIFont systemFontOfSize:10];
    [footerView addSubview:contAuthorIntroduceLabel];
    //赞 背景
    UIImageView* likeBgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth-80, 50, 80, 30)];
    UIImage* image = [UIImage imageNamed:@"home_likeBg"];
    image = [image stretchableImageWithLeftCapWidth:40 topCapHeight:0];     //拉伸
    likeBgImgView.contentMode = UIViewContentModeScaleAspectFit;
    likeBgImgView.image = image;
    //likeBgImgView.backgroundColor = [UIColor redColor];
    likeBgImgView.userInteractionEnabled = YES;
    [footerView addSubview:likeBgImgView];
    //赞 心
    UIButton* likeBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 8, 15, 15)];
    [likeBtn setImage:[UIImage imageNamed:@"home_like"] forState:UIControlStateNormal];
    [likeBtn setImage:[UIImage imageNamed:@"home_like_hl"] forState:UIControlStateSelected];
    [likeBtn addTarget:self action:@selector(likeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [likeBgImgView addSubview:likeBtn];
    //赞数量
    likeCountLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(30, 8, likeBgImgView.bounds.size.width-20, 15)];
    NSString* likeCount = _dataDic[@"strPraiseNumber"];
    likeCountLabel.text = likeCount;
    likeCountLabel.textAlignment = NSTextAlignmentCenter;
    likeCountLabel.font = [UIFont systemFontOfSize:8];
    [likeCountLabel loadLabelColor];
    [likeBgImgView addSubview:likeCountLabel];
}

#pragma mark - tableViewDelegateDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString* content = _dataDic[@"strAnswerContent"];        //正文
    NSArray* array = [content componentsSeparatedByString:@"<br>"];     //以此为分割线，分割成各个段落，每个段落是一个cell
    
    return array.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    QuestionTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kQuestionTableViewCellId forIndexPath:indexPath];
    
    NSString* content = _dataDic[@"strAnswerContent"];        //正文
    NSArray* array = [content componentsSeparatedByString:@"<br>"];     //以此为分割线，分割成各个段落，每个段落是一个cell
    NSString* str = array[indexPath.row];
    
    cell.contentStr = str;
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* content = _dataDic[@"strAnswerContent"];
    NSArray* array = [content componentsSeparatedByString:@"<br>"];
    if (array.count > 0) {
        NSString* str = array[indexPath.row];
        CGFloat height = [Tools contentSizeWithMaxWidth:kWidth-20 text:str fontOfSize:14].height;
        
        return height+10;
    }
    
    return 80;
}

#pragma mark - btnAction
- (void)likeBtnAction:(UIButton*)button {
    button.selected = !button.selected;
    
    NSInteger likeCount = [likeCountLabel.text integerValue];
    likeCount = button.selected?likeCount+1:likeCount-1;
    likeCountLabel.text = [NSString stringWithFormat:@"%li", (long)likeCount];
}

@end
